package com.zyf.pojo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.joda.time.DateTime;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;

public class EasyExcelTest {

    String path= "C:\\Users\\zyf\\Desktop/";

    private List<User> data(){
        List<User> list = new ArrayList<User>();

            User user = new User();
            user.setCustomName("RIVIERA MAISON");
            user.setDate("I1200879");
            user.setProductNo("JK1700011/211310");
            user.setCnumber("4800PCS");
            user.setFactory("会龙厂");

            SimpleDateFormat sf=new SimpleDateFormat("yyyy/MM/dd");
            try {
                user.setDeliveryPeriod(sf.parse("2018/11/15"));
                user.setShipTime(sf.parse("2018/11/30"));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            user.setTradeTerms("T/T");
            list.add(user);

        return list;
    }
    private List<Goods> data1(){
        List<Goods> list = new ArrayList<Goods>();

        Goods goods = new Goods();
        goods.setCustomName("RIVIERA MAISON");
        goods.setDate("I1200879");
        goods.setProductNo("JK1700011/211310");
        goods.setCnumber("4800PCS");
        goods.setFactory("会龙厂");

        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            goods.setDeliveryPeriod(sf.parse("2018-11-15"));
            goods.setShipTime(sf.parse("2018-11-30"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        goods.setTradeTerms("T/T");
        list.add(goods);

        return list;
    }

    @Test
    public void simpleWrite(){
        String fileName = path+"无模板.xlsx";
        //write(路径，格式类)
        //sheet(表名)
        //doWrite(数据)
        EasyExcel.write(fileName,Goods.class).sheet("测试").doWrite(data1());

    }

    @Test
    public void annotationStyleWrite() {
        String fileName = path+"模板1.xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName, User.class).sheet("模板").doWrite(data());
    }
}
